# -*- coding:utf-8 -*-

from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json


class CollectionDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()

    def add_collection(self, col_info):
        user_id = col_info['user_id']
        item_id = col_info['item_id']
        collection_status = col_info['collection_status']

        sql = "insert into zzh_collection (user_id,item_id,collection_status) VALUES (%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [user_id, item_id, collection_status])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def update_collection(self):
        pass

    def query_col(self, user_id):
        """
        该方法已废弃
        :param user_id:
        :return:
        """
        sql = "select * from zzh_collection WHERE user_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [user_id])
        col_list = cur.fetchall()
        col_json = Table2Json.col_list_to_json(col_list)
        return col_json

    def delete_cols(self, collection_ids):
        cur = self.conn.cursor()
        sql = "delete from zzh_collection WHERE collection_id=%s"
        for ids in collection_ids:
            cur.execute(sql, [ids])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return

    def query_collections(self, user_id):
        """
        收藏车和item表的综合查询
        """
        sql = "select * from zzh_collection col,zzh_item it  WHERE col.user_id=%s AND col.collection_status=1 " \
              "AND col.item_id=it.item_id"
        cur = self.conn.cursor()

        rs = cur.execute(sql, [user_id])
        coList = cur.fetchall()
        cur.close()
        self.conn.close()
        clJson = Table2Json.collection_and_item_to_json(coList)

        return clJson

    def addManyCollections(self, userId, itemIds):
        userIdIn = userId
        collectionStatus = 1
        cur = self.conn.cursor()
        for itemId in itemIds:
            sql = "INSERT INTO zzh_collection (user_id,item_id,collection_status) VALUES (%s,%s,%s)"
            result = cur.execute(sql, [userIdIn, itemId, collectionStatus])

        self.conn.commit()
        cur.close()
        self.conn.close()
        if result:
            return 1
        else:
            return 0

    def logicDeleteCollection(self, userId, itemId):
        userIdIn = userId
        collectionStatus = -1
        cur = self.conn.cursor()
        sql = "UPDATE zzh_collection SET collection_status=%s WHERE user_id=%s AND item_id=%s"
        result = cur.execute(sql, [collectionStatus, userIdIn, itemId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if result:
            return 1
        else:
            return 0

    def queryCollectionInfo(self, userId, itemId):
        cur = self.conn.cursor()
        sql = "select collection_id,collection_status from zzh_collection WHERE user_id=%s AND item_id=%s"
        rs = cur.execute(sql, [userId, itemId])
        collectionIdAndCollectionStatus = cur.fetchall()
        collectionIdAndCollectionStatusJSON = Table2Json.colletionIdAndStatus_list_to_json(
            collectionIdAndCollectionStatus)
        if rs:
            return collectionIdAndCollectionStatusJSON
        else:
            return None
